layui.use(['form', 'upload'], function() {
    var form = layui.form;
    var upload = layui.upload;

    // layer.open({
    //     type: 1,
    //     content: '<div style="padding:4px">' +
    //         '<h3 style="font-size:15px;margin-bottom:10px;">如遇问题请扫码进群咨询或电话咨询</h3>' +
    //         '<h4 style="font-size:14px;color:#000;padding:2px 0;">微信群:</h4>' +
    //         '<div style="text-align:center;"><img src="/index/img/ele_puhv.png?t=' + Math.random() +
    //         '" width="110"></div>' +
    //         '<p style="font-size:14px;color:#000;padding:2px 0;white-space:nowrap;">咨询电话：<br>&nbsp;&nbsp;&nbsp;&nbsp;(0574) 6262 0808</p>' +
    //         '</div>',
    //     skin: 'layui-layer-dir',
    //     area: ['250px', '220px'],
    //     title: '',
    //     offset: 'rb',
    //     shade: false,
    //     success: function(layero, index) {
    //         layer.style(index, {
    //             marginLeft: -10,
    //             marginTop: -10
    //         });
    //     }
    // });

    // 统一社会信用代码
    $('#tyshxxdm').on('input', function() {
        $(this).parents('.ulal').find('.fuvl').addClass('hide');
        if ($(this).val()) {
            // if(new Tyshyxdm().verify($(this).val())) {
            //     $(this).parents('.ulal').find('.kwls').removeClass('hide');
            //     $(this).parents('.ulal').find('.qihr').removeClass('hide');
            // } else {
            //
            //     $(this).parents('.ulal').find('.qihr').removeClass('hide');
            //     $(this).parents('.ulal').find('.kwls').removeClass('hide');
            // }
        }
    });

    $('#lxrsjh').on('input', function() {
        $(this).parents('.ulal').find('.fuvl').addClass('hide');
        var val = $(this).val();
        if (val) {
            if (/^1\d{10}$/.test(val)) {
                $(this).parents('.ulal').find('.kwls').removeClass('hide');
            } else {
                $(this).parents('.ulal').find('.qihr').removeClass('hide');
            }
        }
    });

    $('#lxryx').on('input', function() {
        $(this).parents('.ulal').find('.fuvl').addClass('hide');
        var val = $(this).val();
        if (val) {
            if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(val)) {
                $(this).parents('.ulal').find('.kwls').removeClass('hide');
            } else {
                $(this).parents('.ulal').find('.qihr').removeClass('hide');
            }
        }
    });
    var mmqd = false;
    $('#dlmm').on('input', function() {
        $(this).parents('.ulal').find('.fuvl').addClass('hide');
        var val = $(this).val();

        switch (getLvl(val)) {
            case 0:
                $(this).parents('.ulal').find('.qihr').removeClass('hide');
                mmqd = false;
                break;
            case 1:

                $(this).parents('.ulal').find('.kwlp').removeClass('hide');
                mmqd = false;
                break;
            case 2:

                $(this).parents('.ulal').find('.kwlq').removeClass('hide');
                mmqd = true;
                break;
            case 3:

                $(this).parents('.ulal').find('.kwlr').removeClass('hide');
                mmqd = true;
                break;
            case 4:

                $(this).parents('.ulal').find('.kwls').removeClass('hide');
                mmqd = true;
                break;
        }
    });

    $('#dlmmConfirm').on('input', function() {
        $(this).parents('.ulal').find('.fuvl').addClass('hide');
        var psss = $('#dlmm').val();
        var val = $(this).val();
        if (val) {
            if (val == psss) {
                $(this).parents('.ulal').find('.kwls').removeClass('hide');
            } else {
                $(this).parents('.ulal').find('.qihr').removeClass('hide');
            }
        }
    });


    $('#roleAovy li').click(function() {
        $(this).addClass('on').siblings().removeClass('on');
        $('#inputRoleJjur').val($(this).attr('data-role'));
    });
    //监听提交
    form.on('submit(formBuvq)', function(data) {
        // layer.msg(JSON.stringify(data.field));
        // return false;
    });

    function appendSelection(list, selection) {
        selection.append("<option value=''>无</option>");
        for (var i = 0; i < list.length; i++) {
            selection.append("<option value='" + list[i]['id'] + "'>" + list[i]['text'] + "</option>");
        }
        form.render();
    }

    $.ajax({
        type: 'post',
        url: sdk.baseIP +'mh/app/field/getCombo',
        dataType: 'json',
        data: {
            dictCode: 'XZJD'
        },
        async: true,
        success: function(result) {
            appendSelection(result, $("#regester_xzjd"));
        }
    });
    var randomstr = getRandomStr();
    // 上传营业执照
    upload.render({
        elem: '#register_yyzz_btn', //绑定元素
        url: sdk.baseIP + 'mh/app/file/upFile', //上传接口
        field: 'file',
        accept: 'images',
        data: {
            RD: randomstr + "/"
        },
        size: 2000,
        before: function(obj) {
            layer.load(); //上传loading
        },
        done: function(res) {
            layer.closeAll('loading'); //关闭loading
            //上传完毕回调
            $("#register_yyzz_btn_ok").show();
            $("#register_yyzz_btn_hidden").val(res['msg']);
        },
        error: function() {
            layer.closeAll('loading'); //关闭loading
            layer.msg("上传异常", {
                icon: 5,
                shade: .5
            })
        }
    });

    // 上传征信授权书
    upload.render({
        elem: '#register_sqs_btn', //绑定元素
        url: sdk.baseIP +'app/file/upFile', //上传接口
        field: 'file',
        accept: 'file',
        data: {
            RD: randomstr + "/"
        },
        size: 2000,
        before: function(obj) {
            layer.load(); //上传loading
        },
        done: function(res) {
            layer.closeAll('loading'); //关闭loading
            //上传完毕回调
            $("#register_sqs_btn_ok").show();
            $("#register_sqs_btn_hidden").val(res['msg']);
            $("#register_urand_btn_hidden").val(randomstr);
        },
        error: function() {
            layer.closeAll('loading'); //关闭loading
            layer.msg("上传异常", {
                icon: 5,
                shade: .5
            })
        }
    });

    $("#register_submit").click(function() {
        // var ty = new Tyshyxdm().verify($("#tyshxxdm").val());
        var ty = true;
        var lx = /^1\d{10}$/.test($('#lxrsjh').val());
        // var mm = /^(?![A-Za-z]+$)(?!\d+$)(?![\W_]+$)/.test($("#dlmm").val()) && /.{6,20}/.test($("#dlmm").val());
        var mm = /^(?![A-Za-z]+$)(?!\d+$)(?![\W_]+$)/.test($("#dlmm").val()) && /.{6,20}/.test($(
            "#dlmm").val());
        var zh = /^[A-Za-z0-9_]+$/.test($("#dlzh").val());
        if ($("#tyshxxdm").val() == '' ||
            $("#fddbrxm").val() == '' ||
            $("#fddbrsfz").val() == '' ||
            $("#lxrxm").val() == '' ||
            $("#lxrsjh").val() == '' ||
            $("#dlzh").val() == '' ||
            $("#dlmm").val() == '' ||
            $("#dlmmConfirm").val() == '' ||
            $("#dxyzm").val() == ''
        ) {
            layer.msg("必填项不能为空!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };
        if (!(zh)) {
            layer.msg("登录账户格式不正确!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };
        if (!(lx)) {
            layer.msg("手机号码格式不对!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };
        if (!(mm)) {
            layer.msg("密码强度不够!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };

        if (!(ty && lx && mmqd)) {
            layer.msg("请按要求填写!", {
                icon: 5,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };

        var mmps = $("#dlmm").val();
        var mmcps = $("#dlmmConfirm").val();
        if (mmps != mmcps) {
            layer.msg("2次输入密码不一致!", {
                icon: 5,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        }

        var ps = getObjFromData($("#register_user_info"));
        ps['urand'] = randomstr;

        var obj = getAjaxEntity('mh/app/qyzcxx/add', ps);
        if (obj['status']) {
            layer.open({
                content: '注册成功，请重新登录。',
                closeBtn: 0,
                icon: 1,
                title: false,
                time: 10000,
                closeBtn: 1,
                yes: function(index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    window.location.href = '/zxw/index/index.html'
                },
                end: function() {
                    window.location.href = '/zxw/index/index.html'
                }
            });

        } else {
            // $("#register_vcode").attr("src", "./app/register/getvalidateCode?random=" + Math.random());
            layer.msg(obj['msg'], {
                icon: 5,
                shade: .5,
                time: 10000,
                closeBtn: 1
            })
        }
    });

    function sendmessage(id, second, text) {
        var ele = $('#' + id);
        debugger
        function settime() {
            if (second == 0) {
                ele.attr('disabled', false);
                ele.text(text);
                second = 60;
            } else {
                ele.attr("disabled", true);
                ele.text("重新发送(" + second + ")");
                second--;
                setTimeout(function() {
                    settime(ele)
                }, 1000)
            }
        }
        ele.click(function() {
            settime();
        });
    }

    $("#sendMsgbtn").click(function() {
        var ty = true;
        var lx = /^1\d{10}$/.test($('#lxrsjh').val());
        var mm = /^(?![A-Za-z]+$)(?!\d+$)(?![\W_]+$)/.test($("#dlmm").val()) && /.{6,20}/.test($(
            "#dlmm").val());
        var tel = $("#lxrsjh").val();
        if (!tel || tel == '') {
            layer.msg("请输入手机号码!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        }
        if (!(lx)) {
            layer.msg("手机号码格式不对!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };
        if (!(mm)) {
            layer.msg("密码强度不够!", {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            });
            return;
        };
        var ps = getObjFromData($("#register_user_info"));
        var obj = getAjaxEntity('app/register/sendMsg', ps);
        if (obj['status']) {
            debugger
            layer.msg(obj['msg'], {
                icon: 1,
                shade: .5,
                time: 10000,
                closeBtn: 1
            })
        } else {
            layer.msg(obj['msg'], {
                icon: 2,
                shade: .5,
                time: 10000,
                closeBtn: 1
            })
        }
    });

    $('#dlzh').on('input', function() {
        $(this).parents('.ulal').find('.fuvl').addClass('hide');
        var val = $(this).val();
        if (val) {
            if (/^[A-Za-z0-9_]+$/.test(val)) {
                $(this).parents('.ulal').find('.kwls').removeClass('hide');
            } else {
                $(this).parents('.ulal').find('.qihr').removeClass('hide');
            }
        }
    });

    var GlobalDcit2 = {};

    function dictini(dictStr) {
        var obj = getAjaxEntity('app/field/getComs', {
            ids: dictStr
        });
        obj = obj['dictMap'];
        if (obj) {
            for (var o in obj) {
                GlobalDcit2[o] = obj[o];
            }
        }
    }
    dictini('JRJG');

    function inipage() {
        var selections = 'recommendbank'.split(",");
        var selections_dicts = 'JRJG'.split(",");
        for (var i = 0; i < selections.length; i++) {
            var c = selections_dicts[i];
            appendSelection(GlobalDcit2[c], $("#" + selections[i]));
        }
        form.render();
    }
    inipage();
});
